package com.changgou.web.controller;

import com.changgou.entity.Result;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.pay.feign.WxPayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/wxpay")
public class PayController {

    @Autowired
    private WxPayFeign wxPayFeign;

    @Autowired
    private OrderFeign orderFeign;

    /**
     *微信支付二维码
     * @param orderId
     * @param model
     * @return
     */
    @GetMapping
    public String wxPay(@RequestParam String orderId, Model model) {

        //查询订单
        Order order = orderFeign.findById(orderId).getData();
        if (order == null) {
            return "fail";
        }

        //判断支付状态
        if (!"0".equals(order.getPayStatus())) {
            return "fail";
        }

        //判断支付返回情况
        Map payMap = wxPayFeign.nativePay(orderId, order.getPayMoney()).getData();
        if (payMap == null) {
            return "fail";
        }

        payMap.put("payMoney", order.getPayMoney());
        payMap.put("orderId", orderId);
        model.addAllAttributes(payMap);
        return "wxpay";

    }

    /**
     *
     * @param payMoney
     * @param model
     * @return
     */
    @GetMapping("/topaysuccess")
    public String topaysuccess(@RequestParam String payMoney,Model model){
        model.addAttribute("payMoney",payMoney);
        return "paysuccess";
    }
}
